package logic

import (
	"context"
	"home-mgr/model"

	"home-mgr/homerpc/homerpc"
	"home-mgr/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SpekListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSpekListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SpekListLogic {
	return &SpekListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SpekListLogic) SpekList(in *homerpc.SpekListRequest) (*homerpc.SpekListResponse, error) {
	// todo: add your logic here and delete this line

	var speks []model.Spek
	tx := model.DB.Debug().
		Model(model.Spek{})
	if in.TimeOrder == 1 {
		tx = tx.Order("created_at asc")
	}
	if in.TimeOrder == 2 {
		tx = tx.Order("created_at desc")
	}
	tx.Find(&speks)
	var spekList []*homerpc.SpekList
	for _, spek := range speks {

		list := homerpc.SpekList{
			UserId:  int64(spek.UserId),
			HouseId: int64(spek.HouseId),
			Text:    spek.Text,
		}
		spekList = append(spekList, &list)
	}
	return &homerpc.SpekListResponse{
		List: spekList,
	}, nil
}
